//
//  FilterView.m
//  XDBaseFrameWork
//
//  Created by XD-XY on 9/21/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "FilterView.h"
#import "FilterCell.h"

@implementation FilterView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.dataArray = [[NSMutableArray alloc] init];
        [self initViews];
    }
    return self;
}

-(void)initViews
{
    CGFloat mheight = IOS7?20:0;
    
    
    self.dataArray = [[NSMutableArray alloc] init];
    self.selectIndex = 0;
    
    self.bgView = [UIView initViewRect:self.bounds andBackGroudColor:[UIColor clearColor]];
    
    tableViewBgView = [UIView initViewRect:CGRectMake(0,mheight+UI_NAVIGATION_BAR_HEIGHT,UI_SCREEN_HEIGHT, 40*5) andBackGroudColor:[UIColor blackColor]];
    tableViewBgView.alpha = 0.6;
    [self addSubview:tableViewBgView];
    
    self.sTbableView = [[UITableView alloc] initWithFrame:CGRectMake(0,tableViewBgView.frame.origin.y, self.frame.size.width, tableViewBgView.frame.size.height) style:UITableViewStylePlain];
    _sTbableView.backgroundColor = [UIColor clearColor];
    _sTbableView.bounces = NO;
    _sTbableView.delegate = self;
    _sTbableView.dataSource = self;
    
    [self addSubview:_bgView];
    [self addSubview:_sTbableView];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewClick)];
    [_bgView addGestureRecognizer:tap];
}

#pragma mark
#pragma mark UITableView Delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellName = @"sCell";
    FilterCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil){
        cell = [[FilterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary * dict = [_dataArray objectAtIndex:indexPath.row];
    cell.myLB.text = dict[@"SceneName"];
    
    if (indexPath.row == _selectIndex){
        DDLOG(@"selectindex = %d",_selectIndex);
        cell.myLB.textColor = UIColorFromRGB(0xffb619);
    }else{
        cell.myLB.textColor = [UIColor whiteColor];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectIndex = indexPath.row;
    NSDictionary * dict = [_dataArray objectAtIndex:indexPath.row];
    [_delegate whileTheViewIsHidden:dict[@"SceneName"] andID:dict[@"SceneID"] isReflash:YES];
    self.hidden = YES;
}

#pragma mark
#pragma mark 重写set dataarray 方法
-(void)setDataArray:(NSMutableArray *)dataArray
{
    if(_dataArray){
        _dataArray = nil;
    }
    _dataArray =dataArray;
    CGRect aRect = tableViewBgView.frame;
    aRect.size.height = 40*[_dataArray count];
    tableViewBgView.frame = aRect;
    
    CGRect bRect = _sTbableView.frame;
    bRect.size.height = aRect.size.height;
    _sTbableView.frame = bRect;
    
    [self.sTbableView reloadData];
}

-(void)bgViewClick
{
    NSDictionary * dict = [_dataArray objectAtIndex:_selectIndex];
    [_delegate whileTheViewIsHidden:dict[@"SceneName"] andID:dict[@"SceneID"] isReflash:NO];
    self.hidden = YES;
}

#pragma mark
#pragma mark 隐藏View
-(void)hiddenTheView
{
    
    self.hidden = YES;
}

#pragma mark
#pragma mark 显示View
-(void)showTheView
{
    [_sTbableView reloadData];
    self.hidden = NO;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
